package com.biubiushop.IO;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;
import java.util.Set;

/**
 * @author chenjia
 * @date 2019/7/10
 **/
public class IOInputStream {

    public static void main(String[] args) throws Exception {

        readFile();
    }

    public static void readStreamFile() throws Exception {
        File file = new File("E:\\YoudaoNote\\File\\out.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStream inputStream = fileInputStream;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[10];
        int i = 0;
        while ((i = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, i);
        }
        String s = byteArrayOutputStream.toString();
        System.out.println(s);
    }

    public static void readFile() throws Exception {
        File file = new File("E:\\YoudaoNote\\File\\out.txt");
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String s = null;
        while ((s = bufferedReader.readLine()) != null) {
            System.out.println(s);
        }
        bufferedReader.close();

    }

    public static void write() throws IOException {
        File newFile = new File("E:\\XXX\\YYY\\ZZZ.txt");
        newFile.getParentFile().mkdirs();
        newFile.createNewFile();
        File oldFile = new File("E:\\YoudaoNote\\File\\out.txt");
        BufferedReader bufferedReader = new BufferedReader(new FileReader(oldFile));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(newFile));
        String s = null;
        while ((s = bufferedReader.readLine()) != null) {
            bufferedWriter.write(s);
        }
        bufferedReader.close();
        bufferedWriter.close();
    }

    public static void writeByStream() throws IOException {
        File newFile = new File("E:\\XXX\\YYY\\ZZZ.txt");
        File oldFile = new File("E:\\YoudaoNote\\File\\out.txt");
        FileInputStream inputStream = new FileInputStream(oldFile);
        BufferedInputStream bif = new BufferedInputStream(inputStream);

        FileOutputStream outputStream = new FileOutputStream(newFile);
        byte[] bytes = new byte[100];
        int len = -1;
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes);
        }
        inputStream.close();
        outputStream.close();
    }

    public static void romdonWrite() throws Exception {

        try {
            RandomAccessFile raf = new RandomAccessFile("E:\\YoudaoNote\\File\\Source.txt", "rw");
            String str = "abcdefg";
            long skip = 7;
            if (skip < 0 || skip > raf.length()) {
                System.out.println("跳过字节数无效");
                return;
            }
            byte[] b = str.getBytes();
            raf.setLength(raf.length() + b.length);
            for (long i = raf.length() - 1; i > b.length + skip - 1; i--) {
                raf.seek(i - b.length);
                byte temp = raf.readByte();
                raf.seek(i);
                raf.writeByte(temp);
            }
            raf.seek(skip);
            raf.write(b);
            raf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void nioWrite() throws Exception {

        RandomAccessFile raf = new RandomAccessFile("E:\\YoudaoNote\\File\\Source.txt", "rw");
        FileChannel fileChannel = raf.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(100);
        int byteRead = fileChannel.read(byteBuffer);
        while (byteRead != -1) {
            byteBuffer.flip();
            System.out.println("Read " + byteRead);
            while (byteBuffer.hasRemaining()) {
                System.out.print((char) byteBuffer.get());
            }
            byteBuffer.clear();
            byteRead = fileChannel.read(byteBuffer);
        }
        raf.close();

    }

    public static void selector() throws Exception {
        int port = 9999;
        ServerSocketChannel channel = ServerSocketChannel.open();
        channel.configureBlocking(false);
        channel.socket().bind(new InetSocketAddress(port)); // bind address on port

        Selector selector = Selector.open(); // create a selector
        SelectionKey selKey = channel.register(selector, SelectionKey.OP_ACCEPT); // regist selector upon channel for interest `accept` event

        while (true) {
            int readyChannel = selector.select();
            if (readyChannel == 0) {
                continue;
            }
            Set selectedKeys = selector.selectedKeys();
            Iterator keyIterator = selectedKeys.iterator();
            while (keyIterator.hasNext()) {
                SelectionKey selectionKey = (SelectionKey) keyIterator.next();
                if (selectionKey.isAcceptable()) {
                    System.out.println("key is acceptable");
                } else if (selectionKey.isConnectable()) {
                    System.out.println("key is connectable");
                } else if (selectionKey.isReadable()) {
                    System.out.println("key is readable");
                } else if (selectionKey.isWritable()) {
                    System.out.println("key is writable");
                }
            }
            keyIterator.remove();
        }
    }


//    public static void test01() throws IOException {
//        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
//        System.out.println("请输入一个字符");
//        char c;
//        c = (char) bufferedReader.read();
//        System.out.println("你输入的字符为"+c);
//    }
//
//    public static void test02() throws IOException {
//        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
//        System.out.println("请输入一个字符，按 q 键结束");
//        char c;
//        do {
//            c = (char) bufferedReader.read();
//            System.out.println("你输入的字符为"+c);
//        } while (c != 'q');
//    }
//
//    public static void test03() throws IOException {
//        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
//        System.out.println("请输入一行字符");
//        String str = bufferedReader.readLine();
//        System.out.println("你输入的字符为" + str);
//    }
}
